﻿using System;
using System.Collections.Generic;
using MvvmFx.Common.ExtensionMethods;

namespace MvvmFx.Common.ViewModels.Behaviors.Observable
{
    public partial class CollectionObserver<TPropertySource>
    {
        #region Add/Remove items in the collection

        void CollectionItemRemoved(object sender, ItemEventArgs<TPropertySource> e)
        {
            //Note: Replaced as there is no support for RemoveAll. "_propertySourceRefs.RemoveAll((item => item.Target == e.Item));"

            List<WeakReference> weakReferencesToRemove = _propertySourceRefs.FindAll(item => item.Target == e.Item);
            foreach (WeakReference weakReference in weakReferencesToRemove)
            {
                _propertySourceRefs.Remove(weakReference);
            }
        }

        #endregion
    }
}
